package top.lilele.adminSystem.utils;

import io.minio.*;
import io.minio.http.Method;
import io.minio.messages.Item;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

/**
 * minio工具类
 *
 * @author lilele
 */
@Component
@Slf4j
public class MinioUtil {

    @Resource
    private MinioClient minioClient;

    /**
     * 创建桶
     *
     * @param bucketName 桶名
     */
    @SneakyThrows
    public void makeBucket(String bucketName) {
        minioClient.makeBucket(MakeBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 文件上传
     *
     * @param multipartFile 文件
     * @param bucketName    桶名
     * @param fileName      文件名
     */
    @SneakyThrows
    public void upload(MultipartFile multipartFile, String bucketName, String fileName) {
        InputStream inputStream = multipartFile.getInputStream();
        minioClient.putObject(PutObjectArgs.builder()
                .bucket(bucketName)
                .object(fileName)
                .stream(inputStream, inputStream.available(), -1)
                .contentType(multipartFile.getContentType())
                .build()
        );
    }

    /**
     * 生成一个get请求可以访问的url
     *
     * @param bucketName 桶名
     * @param fileName   对象名
     * @param expiry     到期时长
     * @return
     */
    @SneakyThrows
    public String generateGetUrl(String bucketName, String fileName, Integer expiry) {
        String url = minioClient.getPresignedObjectUrl(GetPresignedObjectUrlArgs.builder().bucket(bucketName).object(fileName).expiry(expiry).method(Method.GET).build());
        return url;
    }

    /**
     * 上传文件流到minio
     *
     * @param inputStream
     * @param bucketName
     * @param fileName
     */
    @SneakyThrows
    public void uploadStream(InputStream inputStream, String bucketName, String fileName) {
        minioClient.putObject(PutObjectArgs.builder().bucket(bucketName).
                object(fileName).stream(inputStream, inputStream.available(), -1).build());
    }

    /**
     * 返回文件流
     *
     * @param bucketName
     * @param fileName
     * @return
     */
    @SneakyThrows
    public GetObjectResponse getObjects(String bucketName, String fileName) {
        GetObjectResponse object = minioClient.getObject(GetObjectArgs.builder().bucket(bucketName).object(fileName).build());
        return object;
    }

    /**
     * 删除桶
     *
     * @param bucketName 桶名
     */
    @SneakyThrows
    public void removeBucket(String bucketName) {
        minioClient.removeBucket(RemoveBucketArgs.builder().bucket(bucketName).build());
    }

    /**
     * 删除对象
     *
     * @param bucket   桶名
     * @param fileName 文件名
     */
    @SneakyThrows
    public void deleteObj(String bucket, String fileName) {
        minioClient.removeObject(RemoveObjectArgs.builder().bucket(bucket).object(fileName).build());
    }

    /**
     * 下载文件
     *
     * @param bucketName 桶名
     * @param fileName   文件名
     */
    @SneakyThrows
    public GetObjectResponse download(String bucketName, String fileName) {
        GetObjectArgs objectArgs = GetObjectArgs.builder().bucket(bucketName)
                .object(fileName).build();
        GetObjectResponse getObjectResponse = minioClient.getObject(objectArgs);
        return getObjectResponse;
    }

    /**
     * 列出桶下的所有对象
     *
     * @param bucketName 桶名
     * @return
     */
    @SneakyThrows
    public List<Item> listObjects(String bucketName) {
        Iterable<Result<Item>> results = minioClient.listObjects(ListObjectsArgs.builder().bucket(bucketName).build());
        List<Item> items = new ArrayList<>();
        for (Result<Item> result : results) {
            items.add(result.get());
        }
        return items;
    }
}
